<?php
namespace Admin\Controller;
use Common\Controller\MyInitController;
class InclassConfController extends MyInitController {
    public function index($test_num = 1){
		/*=========定义变量=======*/
		$openid 	= $this->openid;
		$Exercise 	= D("exercise");
		$Testans   	= D('testans');  
		$Time   	= D('time');  
		
		$test_num	=	str_replace(" (开启中)",'',$test_num);
		/*=========获取习题一览=======*/
		$exer_info  = $Exercise->distinct(true)->field('test_num')->select();
		/*=========计算平均正确率并获取状态=======*/
		for($k=0;$k<count($exer_info);$k++){							
			$exer_info[$k]["rate"] = $this->getAverRate($exer_info[$k]["test_num"]);							
			$exer_info[$k]["test_num"] = $exer_info[$k]["test_num"];
			$exer_info[$k]["statue"]	= (($Time->where("test_num = '{$exer_info[$k]["test_num"]}'")->getField("isopen") == 1)? ' (开启中)':'');							
		}
		
		if($exer_item_add = $this->isExercise($openid,$test_num)){
			$exer_item  = $Exercise->distinct(true)->field('id,test_num,question,optA,optB,optC,optD,que_analysis,rightans')->where("test_num = $test_num")->order("id")->select();			
			$i			= 0;

			foreach($exer_item_add as $a){	
				$exer_item[$i] = array_merge($exer_item[$i], $a); 
				$i++;					
			}
		}else{
			$exer_item  = $Exercise->distinct(true)->field('id,test_num,question,optA,optB,optC,optD')->where("test_num = $test_num")->select();
			$this->assign("isExercise",0);
		}		
		$isOpen	=	$Time->where("test_num = $test_num")->getField("isopen");
		/*=========获取习题题目=======*/				
		$this->assign("exer_info",$exer_info);
		$this->assign("openid",$openid);
		$this->assign("exer_item",$exer_item);
		$this->assign("isOpen",$isOpen);
				
		$this->display("InclassConf/index");	
    }
	public function isExercise($openid,$test_num){
		return true;
	}
	public function getAverRate($test_num){
		//$Class	=	D("class");
		$_Testans   = D('testans');
		$current=	'网络'.substr(date('Y', time()),2,4);
		$current='网络'.'14';//测试
		$student=	$_Testans->where("LEFT(class,4)= '$current' AND test_num = $test_num")->select();
		// var_dump($student);
		// exit($current);
		
		$rate = 0;
		$total = count($student);
		$right	=	0;
		foreach($student as $s){											
			$s["right_lab"]? $right++:0;
		}	
		$rate	=	$right/$total*100? $right/$total*100:0;		 				
		$rate	=	substr($rate,0,2);
		
		return $rate;							
		
	}
	public function studentExercise($openid=0,$test_num=1){
		$_Exercise 	= D("exercise");
		$_Testans   = D('testans');
		
		$counts 	= $_Exercise->field('id,test_num')->where("test_num = $test_num")->count();		
		$result 	= $_Testans->where("openid='{$openid}' AND test_num = {$test_num}")->field('id,testid,test_num,answer,right_lab,time')->limit($counts)->order("id")->select();
		
		return $result;
	}
	public function switcher(){
		$openid	=	$this->openid;
		$test_num_id	=	I("post.test_num_id");
		$isOpen	=	I("post.isopen");
		$isOpen=	($isOpen == "0")? 1:0;
		
		//echo $openid;
		
		$Time   	= 	D('time');  
		$result		=	$Time->where("test_num = $test_num_id")->setField("isopen",$isOpen);
		if($result) {				
			$this->success("操作完成！");  				
		}else{
			//$this->error($Time->getError());
			$this->error($Time->getError());
		}	
	}
	public function setTime(){
		if(IS_AJAX){
			/*=========定义变量=======*/
            $timeid = I('post.timeid');          			
			$time	= I("post.time");
			if($time == '') $time = null;
			$switch	= array("","updatastart","updataend","resubmitend","responsestart","responseend");
			$List   = D("list");
			
			$tmp	=	split("_",$timeid);
			$id		=	$tmp[0];
			$column	=	$switch[$tmp[1]];
			
			$List->$column = $time;
			
			$result = $List->where("id = $id")->save();
			if($result) {				
				$this->ajaxReturn($tmp);  				
			}else{
				$this->error($List->getError());
			}
		}
		else $this->error('非法的请求方式'); 	
	}
	
	
	
}




?>